// pages/addAccount/addAccount.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time:60,
    send:false,
    region:['请选择开户地区'],
    bankList:[
      {
        name:'工商银行',
        id:'1002',
      },
      {
        name:'农业银行',
        id:'1005',
      },
      {
        name:'建设银行',
        id:'1003',
      },
      {
        name:'中国银行',
        id:'1026',
      },
      {
        name:'交通银行',
        id:'1020',
      },
      {
        name:'招商银行',
        id:'1001',
      },
      {
        name:'邮储银行',
        id:'1066',
      },
      {
        name:'民生银行',
        id:'1006',
      },
      {
        name:'平安银行',
        id:'1010',
      },
      {
        name:'中信银行',
        id:'1021',
      },
      {
        name:'浦发银行',
        id:'1004',
      },
      {
        name:'兴业银行',
        id:'1009',
      },
      {
        name:'光大银行',
        id:'1022',
      },
      {
        name:'广发银行',
        id:'1027',
      },
      {
        name:'华夏银行',
        id:'1025',
      },
      {
        name:'宁波银行',
        id:'1056',
      },
      {
        name:'北京银行',
        id:'4836',
      },
      {
        name:'上海银行',
        id:'1024',
      },
      {
        name:'南京银行',
        id:'1054',
      },
      {
        name:'长沙银行',
        id:'4216',
      },
      {
        name:'中原银行',
        id:'4753',
      },
      {
        name:'企业银行（中国）',
        id:'4761',
      },
      {
        name:'顺德农商银行',
        id:'4036',
      },
      {
        name:'衡水银行',
        id:'4752',
      },
      {
        name:'广西壮族自治区农村信用社',
        id:'4113',
      },
      {
        name:'深圳农村商业银行',
        id:'4076',
      },
      {
        name:'江苏紫金农村商业银行股份有限公司',
        id:'4072',
      },
      {
        name:'北京中关村银行股份有限公司',
        id:'4769',
      },
      {
        name:'南洋商业银行（中国）有限公司 ',
        id:'4763',
      },
    ],
    id:null,
    open_bank:"请输入开户行",
    open_bankid:"null",
    name:'',
    bank_code:'',
    mobile:'',
    code:'',
    time:60,
    send:false,
    // 流程状态 1:添加账户 2:修改账户
    status:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInfo()
  },

  
  getInfo(){
    JsonPost('bank/info').then(res=>{
      if (res.data.id) {
        this.setData({
          open_bank:res.data.open_bank,
          name:res.data.name,
          bank_code:res.data.bank_code,
          mobile:res.data.mobile,
          id:res.data.id,
          status:2
        })
      }
    })
  },

  
  setValue(e) {
    let str = e.currentTarget.dataset.row
    this.setData({
      [str]: e.detail.value
    })
  },

  
  sendCode() {
    if (this.data.send) {
      return
    }
    if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.mobile))) {
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    let data={
      'mobile':this.data.mobile
    }
    JsonPost('user/sendCode',data).then(res=>{
      if (res.code==0) {
        this.setData({
          send:true
        })
        wx.showToast({
          title: '已发送,请注意查收',
          icon: 'none',
          duration: 2000
        })
        let that=this
        wpxtime= setInterval(() => {
          if (that.data.time<1) {
            clearInterval(wpxtime)
            that.setData({
              send:false,
              time:60
            })
            return false
          }
          that.setData({
            time:that.data.time-1
          })
        }, 1000);
      }
    })
  },
  goOn(){
    if (!this.data.mobile||!this.data.code) {
      wx.showToast({
        title: '请填写手机号和验证码',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let data={
      'mobile':this.data.mobile,
      'code':this.data.code,
      'open_bank':this.data.open_bank,
      'name':this.data.name,
      'bank_code':this.data.bank_code,
      'id':this.data.id,
    }
    if (this.data.status==1) {
      JsonPost('user/add-bank',data).then(res=>{
        if (res.code==0){
          wx.showToast({
            title: '添加成功',
            icon: 'none',
            duration: 2000
          })
          clearInterval(wpxtime)
          wx.navigateBack()
        }
      })
    }else{
      JsonPost('user/update-bank',data).then(res=>{
        if (res.code==0){
          wx.showToast({
            title: '修改成功',
            icon: 'none',
            duration: 2000
          })
          clearInterval(wpxtime)
          wx.navigateBack()
        }
      })
    }
  },

  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },

  bindPickerChange: function (e) {
    this.setData({
      open_bank: this.data.bankList[e.detail.value].name,
      open_bankid: this.data.bankList[e.detail.value].id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})